<template>
  <div class="app-container">
    <div class="app-search">
      <el-row :gutter="24">
        <el-col :span="18">
          <el-form :inline="true" class="demo-form-inline">
            <el-form-item label="角色名称">
              <el-input v-model="search.name" placeholder="请输入用户名">
              </el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="search.status" placeholder="请选择状态">
                <el-option
                  label="启用"
                  value="1">
                </el-option>
                <el-option
                  label="禁用"
                  value="2">
                </el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6" class="right">
          <el-button type="primary" icon="el-icon-search" size="small" @click="searchChange">搜索</el-button>
          <el-button icon="el-icon-delete" size="small" @click="reset">重置</el-button>
        </el-col>
      </el-row>
    </div>
    <div class="app-table">
      <el-row :gutter="24" type="flex" justify="between" class="padding">
        <el-col :span="12" class="titles">
          <span class="title">角色列表</span>
        </el-col>
        <el-col :span="12" class="right">
          <el-button type="primary" @click="handleAdd" size="medium">新增角色</el-button>
        </el-col>
      </el-row>
      <el-table
        v-loading="listLoading"
        :data="list"
        row-key="id"
        border
        default-expand-all
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        :height="value === '100%' ? 'calc(100vh - 55px - 25px - 310px)' : ''"
      >
        <el-table-column
          type="index"
          width="50">
        </el-table-column>
        <el-table-column
          prop="name"
          label="角色名称"
          width="180"
        />
        <el-table-column
          prop="roleDescribe"
          label="角色描述"
        />
        <el-table-column
          prop="status"
          label="启用状态"
          width="80"
        >
          <template slot-scope="scope">
            <el-switch
              class="demo"
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="启用"
              inactive-text="禁用"
              :active-value="1"
              :inactive-value="2"
              @change="handleEditStatus(scope.$index, scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="count > 0"
        :total="count"
        :page.sync="search.page"
        :limit.sync="search['limit']"
        @pagination="list"
      />
    </div>
    <el-dialog :title="title" width="40%" :visible.sync="dialogForm" @close="colse">
      <el-form ref="form" :model="form" size="mini" :rules="rules">
        <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-radio-group v-model="form.status" style="margin-bottom: 20px;" >
            <el-radio-button :label="1">启用</el-radio-button>
            <el-radio-button :label="2">禁用</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="描述" :label-width="formLabelWidth">
          <el-input v-model="form.describe" placeholder="请输入描述"></el-input>
        </el-form-item>
        <el-form-item label="角色" :label-width="formLabelWidth">
          <el-tree
            :data="options"
            ref="tree"
            show-checkbox
            node-key="id"
            :default-checked-keys="form.menuIds"
            :props="defaultProps">
          </el-tree>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogForm = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import pagination from '@/components/Pagination/index'
import { getAllMenu } from '@/api/admin/mune'
import { editStatus, getRolePage, roleRemove, saveAdminRole } from '@/api/admin/role'

export default {
  components: {
    pagination
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      value: '100%',
      list: null,
      listLoading: true,
      dialogForm: false,
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      search: {
        limit: 10,
        name: '',
        page: 1,
        status: ''
      },
      form: {
        name: '',
        status: 1,
        menuIds: [],
        describe: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ]
      },
      title: '',
      options: [],
      count: 0,
      formLabelWidth: '120px',
      optionProps: {
        value: 'id',
        label: 'name',
        children: 'children',
        checkStrictly: true
      }
    }
  },
  created() {
    this.getList()
    this.getMenu()
  },
  methods: {
    handleAdd() {
      this.dialogForm = true
      this.title = '新增角色'
    },
    handleEdit(index, row) {
      this.dialogForm = true
      this.form = JSON.parse(JSON.stringify(row))
      this.form.describe = this.form.roleDescribe
      this.title = '修改角色'
    },
    searchChange() {
      this.getList()
    },
    reset() {
      this.search = {
        limit: 10,
        name: '',
        page: 1,
        status: ''
      }
      this.getList()
    },
    colse() {
      this.form = {
        name: '',
        status: 1,
        menuIds: [],
        describe: ''
      }
      this.$refs.tree.setCheckedKeys([])
    },
    submit() {
      this.$refs.form.validate((vaild,done) => {
        if (vaild) {
          const options = this.$refs.tree.getCheckedNodes()
          console.log(JSON.stringify(options));
          options.forEach(item => {
            this.form.menuIds.push(item.id)
          })
          saveAdminRole(this.form).then(response => {
            this.getList()
            this.dialogForm = false
            this.form = {
              name: '',
              status: 1,
              menuIds: [],
              describe: ''
            }
            this.$message({
              showClose: true,
              message: response.message,
              type: 'success'
            })
          })
        }
      })
    },
    handleDelete(index, row) {
      this.$confirm(
        '删除',
        '删除', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          roleRemove(row.id).then(response => {
            this.getList()
            this.$message({
              showClose: true,
              message: response.message,
              type: 'success'
            })
          })
        })
    },
    handleEditStatus(index, row) {
      editStatus({ id: row.id,status: row.status }).then(response => {
        this.getList()
        this.$message({
          showClose: true,
          message: response.message,
          type: 'success'
        })
      })
    },
    getMenu() {
      getAllMenu().then(response => {
        this.options = response.data
      })
    },
    getList() {
      this.listLoading = true
      getRolePage(this.search).then(response => {
        this.list = response.data.records
        this.count = response.data.total
        this.listLoading = false
      })
    }
  }
}
</script>

<style>
.demo .el-switch__label {
  position: absolute;
  display: none;
  color: #fff !important;
}
/*打开时文字位置设置*/
.demo .el-switch__label--right {
  z-index: 1;
}
/* 调整打开时文字的显示位子 */
.demo .el-switch__label--right span{
  margin-left: 9px;
}
/*关闭时文字位置设置*/
.demo .el-switch__label--left {
  z-index: 1;
}
/* 调整关闭时文字的显示位子 */
.demo .el-switch__label--left span{
  margin-left: 22px;
}
/*显示文字*/
.demo .el-switch__label.is-active {
  display: block;
}
/* 调整按钮的宽度 */
.demo.el-switch .el-switch__core,
.el-switch .el-switch__label {
  width: 60px !important;
  margin: 0;
}

</style>

<style>
/deep/.el-form-item{
  margin-bottom: 0 !important;
}
</style>

<style scoped>
.title{
  padding-left: 7px;
  font-size: 16px;
  font-weight: 500;
  line-height: 24px;
}
.right{
  display: flex;
  justify-content: flex-end;
}
.padding{
  margin-bottom: 20px;
}
.titles{
  display: flex;
  align-items: center;
}
.flex{
  padding-top: 20px;
}
.app-container{
  background: #f0f2f5;
}
.app-search{
  background-color: #FFFFFF;
  padding: 20px;
  margin-bottom: 20px;
}
.app-table{
  background-color: #FFFFFF;
  padding: 20px;
}
</style>
